Compressing and Uncompressing Data in Eiffel using Zlib

by Javier Velilla (modified: 2014 Feb 12)

Eiffel Zlib is a wrapper on top of C Zlib library, the Eiffel code is based on an old Eiffel library updated to the latest Eiffel version. Zlib is a C library use for data compression, in most of the cases you will find a Zlib version for your target platform.

Note: the Zlib library is under development so maybe the API change in the future. Don’t hesitate to send suggestions and improvements.

Getting Started

Checkout the library.

svn co https://svn.eiffel.com/eiffelstudio/trunk/Src/unstable/library/compression/zlib/

The library will be available with the next EiffelStudio release 14.05

API overview

The current library does not support everything, It comes with all the basic functions (deflate, deflateEnd, inflate, inflateEnd and version), and the most common utility functions (gzread, gzwrite, gzclose, gzopen).

Also the library has classes to work with ZLIB_COMPRESS and ZLIB_UNCOMPRESS

ZLIB_COMPRESS class hierarchy.

ZLIB_UNCOMPRESS class hierarchy.

Examples Simple examples using the new interface

IO_MEDIUM stream_example_file_to_file2 -- Example using IO_MEDIUM. local zi: ZLIB_IO_MEDIUM_UNCOMPRESS zo: ZLIB_IO_MEDIUM_COMPRESS l_file: FILE l_src_file: FILE l_new_file: FILE do create {RAW_FILE}l_file.make_create_read_write ("new_test2.txt") create {RAW_FILE}l_src_file.make_open_read (source_file) create {RAW_FILE}l_new_file.make_create_read_write ("new_file2.txt") create zo.io_medium_stream (l_file) zo.put_io_medium (l_src_file) create {RAW_FILE}l_file.make_open_read ("new_test2.txt") create zi.io_medium_stream (l_file) zi.to_medium (l_new_file) print ("%NBytes compresses:" + zo.total_bytes_compressed.out) print ("%NBytes uncompresses:" + zi.total_bytes_uncompressed.out) end

STRING stream_example_string_2 local di: ZLIB_STRING_UNCOMPRESS dc: ZLIB_STRING_COMPRESS input_string: STRING output_string: STRING do input_string:= "[ zlib is designed to be a free, general-purpose, legally unencumbered -- that is, not covered by any patents -- lossless data-compression library for use on virtually any computer hardware and operating system. The zlib data format is itself portable across platforms. Unlike the LZW compression method used in Unix compress(1) and in the GIF image format, the compression method currently used in zlib essentially never expands the data. (LZW can double or triple the file size in extreme cases.) zlib's memory footprint is also independent of the input data and can be reduced, if necessary, at some cost in compression. A more precise, technical discussion of both points is available on another page. zlib was written by Jean-loup Gailly (compression) and Mark Adler (decompression). Jean-loup is also the primary author/maintainer of gzip(1), the author of the comp.compression FAQ list and the former maintainer of Info-ZIP's Zip; Mark is also the author of gzip's and UnZip's main decompression routines and was the original author of Zip. Not surprisingly, the compression algorithm used in zlib is essentially the same as that in gzip and Zip, namely, the `deflate' method that originated in PKWARE's PKZIP 2.x. Mark and Jean-loup can be reached by e-mail at zlib email address. Please read the FAQ and the manual before asking us for help. We are getting too many questions which already have an answer in the zlib documentation. Greg, Mark and/or Jean-loup will add some more stuff here when they think of something to add. For now this page is mainly a pointer to zlib itself and to related links. Note that the deflate and zlib specifications both achieved official Internet RFC status in May 1996, and zlib itself was adopted in version 1.1 of the Java Development Kit (JDK), both as a raw class and as a component of the JAR archive format. The lovely zlib-vise image above was provided courtesy of Bruce Gardner, art director of Dr. Dobb's Journal. It appears in Mark Nelson's article in the January 1997 issue (see below). ]" create output_string.make_empty create dc.string_stream (output_string) dc.put_string (input_string) create di.string_stream (output_string) check same_string: input_string.same_string (di.to_string) end print ("%NBytes compresses:" + dc.total_bytes_compressed.out) print ("%NBytes uncompresses:" + di.total_bytes_uncompressed.out) end

MEMORY stream_example_memory local zi: ZLIB_MEMORY_UNCOMPRESS zo: ZLIB_MEMORY_COMPRESS input_buffer: ARRAY[NATURAL_8] output_buffer: MANAGED_POINTER new_buffer: ARRAY[NATURAL_8] output: MANAGED_POINTER do input_buffer := <<1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,49,90, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,49,90, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,49,90, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,49,90, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,49,90, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,49,90, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,49,90, 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,49,90 >> create output_buffer.make (128) create zo.memory_stream (output_buffer) zo.put_memory (create {MANAGED_POINTER}.make_from_array (input_buffer)) create zi.memory_stream (output_buffer) output := zi.to_memory new_buffer := output.read_array (0, output.count) new_buffer.compare_objects input_buffer.compare_objects check same_content: new_buffer.is_equal (input_buffer) end print ("%NBytes compresses:" + zo.total_bytes_compressed.out) print ("%NBytes uncompresses:" + zi.total_bytes_uncompressed.out) end You can check the examples here https://svn.eiffel.com/eiffelstudio/trunk/Src/unstable/library/compression/zlib/example/.